package com.ubing.gxspringboot.gxproxy;

import com.ubing.gxspringboot.gxservice.gxserviceimpl.TestServiceImpl;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import sun.security.util.AuthResources_it;

import java.lang.reflect.Method;

public class CGLibProxy implements MethodInterceptor {

    private Class<?> clazz;
    private String methodName;

    public CGLibProxy(Class<?> clazz, String methodName) {
        this.clazz = clazz;
        this.methodName = methodName;
    }
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        JoinPoint joinPoint = new CGLibJoinPoint(obj, method, args,proxy);
        return clazz.getDeclaredMethod(methodName, JoinPoint.class).invoke(clazz.newInstance(), joinPoint);
    }

    public static Object bind(Object target,String methodName,Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(new CGLibProxy(clazz,methodName));
        // 创建代理类的实例
        return enhancer.create();
    }
}
